package com.tianqu.tms.www.rental.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.RentalInfoDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;;

public class RentalLocationDetailController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();
		RentalLocationViewHelper view = new RentalLocationViewHelper();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			// 租位ID
			long id = Long.valueOf(uc.getParameter("id"));
			Codes codes = Codes.getInstance();
			
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();
			rentalLocationInfo = rentalLocationInfo.searchLocationDetailById(id);
			
			view.setId(id);
			view.setLocationSerial(rentalLocationInfo.getLocationSerial());
			view.setOldLocationSerial(rentalLocationInfo.getLocationSerial());
			view.setRentalType(rentalLocationInfo.getRentalType());
			view.setRentalTypeName(rentalLocationInfo.getRentalTypeName());
			view.setAcreage(StringUtil.getPointBetweenThree(rentalLocationInfo.getAcreage()));
			view.setCarPort(StringUtil.getPointBetweenThree(rentalLocationInfo.getCarPort()));
			view.setRentalAreaName(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, rentalLocationInfo.getArea()));
			view.setRentalStatusName(rentalLocationInfo.getRentalStatusName());
			view.setInsertUserId(rentalLocationInfo.getInsertUserId());
			view.setInsertUserName(rentalLocationInfo.getInsertUserName());
			view.setAddress(rentalLocationInfo.getAddress());
			view.setRentalArea(rentalLocationInfo.getArea());
			if(rentalLocationInfo.getInsertTime() != null){
				view.setInsertTime(DateUtil.DateToString11(rentalLocationInfo.getInsertTime()));
			}
			view.setUpdateUserId(rentalLocationInfo.getUpdateUserId());
			view.setUpdateUserName(rentalLocationInfo.getUpdateUserName());
			if(rentalLocationInfo.getUpdateTime() != null){
				view.setUpdateTime(DateUtil.DateToString11(rentalLocationInfo.getUpdateTime()));
			}
			view.setAnnualTransactionVolume(rentalLocationInfo.getAnnualTransactionVolume());
			RentalContractInfo rentalContractInfo = RentalContractInfo.getInstance();
			List<RentalContractInfo> list = rentalContractInfo.searchRentalContractInfoByLocationSerial(rentalLocationInfo.getLocationSerial());
			
			if (list != null && list.size() > 0) {
				view.setRentalStatusName("已出租");
			} else {
				view.setRentalStatusName("未出租");
			}
			
			if (null != list && list.size() >0) {
				List<RentalLocationViewHelper> rentalLocationViewHelperlist = new ArrayList<RentalLocationViewHelper>();
				//插入操作员
				User user = User.getInstance();
				
				for (int i = 0; i <list.size(); i++) {
					RentalContractInfo locationInfo = new RentalContractInfo();
					RentalLocationViewHelper rentalLocationViewHelper = new RentalLocationViewHelper();
					locationInfo = list.get(i);
					rentalLocationViewHelper.setId(locationInfo.getId());
					rentalLocationViewHelper.setRowNo(locationInfo.getRowNo());
					rentalLocationViewHelper.setSerial(locationInfo.getSerial());
					rentalLocationViewHelper.setStartDate(locationInfo.getStartDate());
					rentalLocationViewHelper.setEndDate(locationInfo.getEndDate());
					rentalLocationViewHelper.setInsertTime(DateUtil.getDateByTimeStampHM(locationInfo.getInsertTime()));
					user = user.getUserInfoById(locationInfo.getInsertUserId());
					rentalLocationViewHelper.setInsertUserName(user.getRealName());
					rentalLocationViewHelper.setLocationSerial(locationInfo.getLocationSerial());
		
					rentalLocationViewHelper.setCssFlag(locationInfo.getCssFlag());

					rentalLocationViewHelperlist.add(rentalLocationViewHelper);
				}
				
				uc.setAttribute(TmsConstants.VIEWHELP, rentalLocationViewHelperlist);
				view.setList(rentalLocationViewHelperlist);
			}
			
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}

